<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  title="拒收"
  :on-close="updateModel"
  :on-cancel="updateModel"
  :on-confirm="updateConfirmModel"
  size="small"

)
  mt-table(
    v-bind="tableProps"
    style="margin-top: 10px;"
  )
  mt-form(
      v-bind="editFormProps"
      style="margin-top: 10px;"
    )
</template>

<script setup lang="ts">
import { useApi } from "@scmt/core-api";
import {
  type FormItemProps,
  type TableProps,
  useNotify,
  type EditFormProps,
} from "@scmt/common-components";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
//添加 Props属性
const props = defineProps({
  selectionRow: {
    type: Array as PropType<SpecimenEntity[]>,
    default: () => [],
  },
});
const $api = useApi();
const $notify = useNotify();
const editingObj = ref<{} | Object>({});

const appmodel = defineModel({
  default: false,
});
const emit = defineEmits(["message-to-parent"]);
const updateModel = () => {
  appmodel.value = false;
};

const { data, request, loadingMap } = useRequest({
  save: (q) => {
    return $api.lis.specimen.batchGenerateBarcode(q).then(() => {
      $notify.success("作废成功");
      emit("message-to-parent");
      appmodel.value = false;
    });
  },
  listAsOptions: () => {
    return $api.lis.specimen.category.listAsOptions();
  },
  tubelistAsOptions: () => {
    return $api.lis.tube.listAsOptions();
  },
});

const updateConfirmModel = () => {
  if (!editingObj?.value?.reason) {
    $notify.error("请选择拒收原因");
    return;
  }
  const specimenIdData = []
    .concat(props.selectionRow)
    .map((item) => {
      return item.id;
    })
    .filter((item) => item != undefined);
  request("save", {
    specimenList: specimenIdData,
    status: 7,
    reasonRejection: editingObj.value?.reason,
    reasonRemarks: editingObj.value?.remark,
  });
};

const editFormProps = computed<EditFormProps>(() => {
  const formItems: FormItemProps[] = [
    {
      prop: "reason",
      label: "原因:",
      required: true,
      as: "select",
      options: {
        loadMethod: () => {
          return [
            {
              label: "标本类型错误",
              value: 1,
            },
            {
              label: "标本容器错误",
              value: 2,
            },
            {
              label: "采集量错误",
              value: 3,
            },
            {
              label: "抗凝标本凝聚",
              value: 4,
            },
            {
              label: "乳糜血的标本",
              value: 5,
            },
            {
              label: "溶血的标本",
              value: 6,
            },
            {
              label: "血培养污染",
              value: 7,
            },
            {
              label: "其他",
              value: 8,
            },
          ];
        },
      },
    },
    {
      prop: "remark",
      label: "备注:",
      required: false,
      options: {
        type: "textarea",
      },
    },
  ];

  const editProps: EditFormProps = {
    items: formItems,
    model: editingObj.value,
    columns: 1,
  };
  return editProps;
});

const tableProps = computed(() => {
  const tableProps: TableProps = {
    data: props.selectionRow.filter((item) => item.id != undefined),
    columns: [
      {
        prop: "patientName",
        label: "姓名",
        width: 120,
      },
      {
        prop: "tubeId",
        label: "试管名称",
        render: ({ row }) => {
          const patientType = data.tubelistAsOptions ?? [];
          let resText = row.tubeId + "";
          if (patientType && patientType.length > 0) {
            let filter = patientType.filter((item) => item.value == row.tubeId);
            if (filter && filter.length > 0) {
              resText = filter[0].label;
            }
          }
          return h(
            "div",

            resText
          );
        },
      },
      {
        prop: "barcode",
        label: "条形码",
        tooltip: true,
      },
      {
        prop: "specimenTypeId",
        label: "标本类型",
        render: ({ row }) => {
          const patientType = data.listAsOptions ?? [];
          let resText = row.specimenTypeId + "";
          if (patientType && patientType.length > 0) {
            let filter = patientType.filter(
              (item) => item.value == row.specimenTypeId
            );
            if (filter && filter.length > 0) {
              resText = filter[0].label;
            }
          }
          return h("div", resText);
        },
      },
      {
        prop: "age",
        label: "采集方式",
      },
    ],
  };
  return tableProps;
});
watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      request("listAsOptions");
      request("tubelistAsOptions");
      editingObj.value = {};
    }
  },
  {
    immediate: true,
  }
);
</script>
